package com.ll.beautymall.ui.nine.view;


import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ll.beautymall.R;
import com.ll.beautymall.adapter.NineAdapter;
import com.ll.beautymall.base.BaseFragment;
import com.ll.beautymall.bean.Category;
import com.ll.beautymall.bean.CategoryList;
import com.ll.beautymall.ui.home.indexfragment.presenter.HeaderPresenter;
import com.ll.beautymall.utils.ConfigPopupWindow;
import com.ll.beautymall.utils.NetUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * A simple {@link Fragment} subclass.
 */
public class NineFragment extends BaseFragment implements INineView, View.OnClickListener {

    @BindView(R.id.home_nav)
    TabLayout homeNav;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.iv_nav_open)
    ImageView ivNavOpen;
    @BindView(R.id.ll_nav)
    LinearLayout llNav;
    @BindView(R.id.home_vp_container)
    ViewPager homeVpContainer;
    @BindView(R.id.ib_empty)
    ImageButton ibEmpty;
    private ConfigPopupWindow configPopupWindow;
    private NineAdapter vpAdapter;
    private View contentView;
    private HeaderPresenter navPresenter;
    private int currentIndex = 0;
    private String cids;
    private String sort;

    public NineFragment() {

    }


    @Override
    protected void initView(ViewGroup container) {
        navPresenter = new HeaderPresenter(this);

        configPopupWindow = new ConfigPopupWindow();
        configPopupWindow.initPopupWindow(getFActivity());

        initTab();

        contentView = LayoutInflater.from(getFActivity()).inflate(R.layout.home_nav_open, container, false);

        ImageView iv_pop_close = (ImageView) contentView.findViewById(R.id.iv_nav_open_pop);
        iv_pop_close.setOnClickListener(this);
        ivNavOpen.setOnClickListener(this);

        vpAdapter = new NineAdapter(getChildFragmentManager());
        homeVpContainer.setAdapter(vpAdapter);
        loadData();

        homeVpContainer.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                currentIndex = position;
            }
        });
    }

    private void loadData() {
        if (NetUtil.hasInternet(getFActivity())) {
            homeVpContainer.setVisibility(View.VISIBLE);
            ibEmpty.setVisibility(View.GONE);

            navPresenter.setNineCategoryData(null);
        } else {
            Toast.makeText(getFActivity(), "无网络...", Toast.LENGTH_SHORT).show();
            homeVpContainer.setVisibility(View.GONE);
            ibEmpty.setVisibility(View.VISIBLE);
            ibEmpty.setOnClickListener(this);
        }
    }

    private void initTab() {
        homeNav.setupWithViewPager(homeVpContainer);
        homeNav.setTabMode(TabLayout.MODE_SCROLLABLE);
        homeNav.setTabTextColors(Color.parseColor("#252827"), Color.parseColor("#CB0000"));
    }

    @Override
    public int getFLayoutId() {
        return R.layout.fragment_nine;
    }

    private void createVpFragmentAndNav(List<Category> categories) {
        List<NineContentFragment> fragments = new ArrayList<>();
        List<String> titles = new ArrayList<>();
        for (int i = 0; i < categories.size(); i++) {
            //首次创建，默认
            cids = categories.get(i).getCids();
            sort = "default";
            NineContentFragment fragment = NineContentFragment.createFragment(cids, sort);
            fragments.add(fragment);
            titles.add(categories.get(i).getTitle());
        }
        vpAdapter.setData(fragments, categories);

        configPopupWindow.setContentView(contentView, tv_title.getWidth(), 740);
        configPopupWindow.setGvData(titles);
        configPopupWindow.setOnPopGvClickLisenter(homeVpContainer);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_nav_open_pop:
                configPopupWindow.disimssPopup();
                break;
            case R.id.iv_nav_open:
                configPopupWindow.showPopup(tv_title);
                break;
            case R.id.ib_empty:
                loadData();
                break;
//            case R.id.fab_back:
//                Toast.makeText(getFActivity(), "fab", Toast.LENGTH_SHORT).show();
//                break;
        }
    }

    @Override
    public void showErrorMsg(String msg) {
        Toast.makeText(getFActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setCategoryListData(CategoryList categoryList) {
        if (categoryList != null) {
            List<Category> categories = categoryList.getCategories();
            createVpFragmentAndNav(categories);
        }
    }
}
